import Member from "@/common/model/Member"
import Service from "@/common/model/Service"
import Demand from "@/common/model/Demand"
import Step from "@/common/model/Step"

/******************** 定义类 ***************************************************************/
/*
    对接类
 */
function Abutment() {
    this.id = 0;
    this.step_abutment_data = {
        step_number: 0,
        status: -1,
        failed_reason: ""
    };
    this.contract = [];
}

// 将对象序列化为一个json对象，可以用于http请求或对象复制
Abutment.prototype.serialize = function() {
    return Object.assign({}, this, {
        process: JSON.stringify(this.process),
        contract_ids: this.contract.map(function(elem){return elem.id}).join(",")
    });
}

// 用json数据为对象属性赋值
Abutment.prototype.parse = function(data) {
    Object.assign(this, data, {
        number:data.number,
        process: data.process ? JSON.parse(data.process) : new Step(),
        demand_member: Member.parse(data.demand_member),
        service_member: Member.parse(data.service_member),
        demand: Demand.parse(data.demand),
        service: data.service ? Service.parse(data.service) : new Service(),
        step_abutment_data: data.step_abutment_data || this.step_abutment_data,
        formated_create_time: new Date(parseInt(data.create_time)).toLocaleDateString(),
    });
}

// ******************** 类静态属性与方法 **********************************************
// 解析json数据，返回一个对象
Abutment.parse = function(data) {
    var o = new Abutment();
    o.parse(data);
    return o;
}

// 解析json数组，返回对象数组
Abutment.parseList = function(list) {
    var len = list.length;
    var arr = new Array(len);
    for(var i = 0; i < len; i++) {
        arr[i] = Abutment.parse(list[i]);
    }
    return arr;
}

// ******************** 数据库存储方法 **********************************************
// Investment.prototype.save = function(callback) {
//     var that = this;
//     var action = this.mode == "add" ? "article_add" : "article_modify";
//     api.post(action, this.serialize(), function(res) {
//         that.id = res.id;
//         if(callback) callback();
//     });
// }



// ******************** 导出 **********************************************
export default Abutment